<?php
/**
 * Created by PhpStorm.
 * User: joe
 * Date: 18-1-16
 * Time: 下午5:09
 */

namespace App\Controllers;

use \System\App;

class Load extends App {
    function __construct() {
        parent::__construct();
    }

    function index() {
        $limit = $this->request->get('limit');
        $offset = $this->request->get('offset');
        $sort = $this->request->get('sort') ?: 'id';
        $order = $this->request->get('order');

        $syslog = $this->db->table('SystemEvents')
                           ->select('ID,DeviceReportedTime,Facility,Priority,FromHost,SysLogTag,LEFT(Message, 90) AS SmallMessage,Message')
                           ->order($sort, $order)
                           ->limit($limit, $offset)
                           ->get()
                           ->result();

        $counts = $this->db->countAll('SystemEvents');

        $this->showJson(['total' => $counts, 'rows' => $syslog]);
    }

    function search() {
        $limit = $this->request->get('limit');
        $offset = $this->request->get('offset');
        $sort = $this->request->get('sort') ?: 'id';
        $order = $this->request->get('order');
        $server = $this->request->get('server');
        $type=$this->request->get('type');
        $key = $this->request->get('key');

        if ($type) {
            $this->db->where('Priority', $type);
        }

        if ($key) {
            $this->db->where('Message', $key,'like');
        }

        if ($server) {
            $this->db->where('Facility', $server);
        }

        $this->db->table('SystemEvents')
                 ->select('ID,DeviceReportedTime,Facility,Priority,FromHost,SysLogTag,LEFT(Message, 90) AS SmallMessage,Message')
                 ->order($sort, $order)
                 ->limit($limit, $offset)
                 ->get();
        $syslog = $this->db->result();
        $this->db->reset();
        if ($type) {
            $this->db->where('Priority', $type);
        }
        if ($key) {
            $this->db->where('Message', $type,'like');
        }
        if ($server) {
            $this->db->where('Facility', $server);
        }
        $counts = $this->db->select('id')
                           ->get('SystemEvents')
                           ->count();

        $this->showJson(['total' => $counts, 'rows' => $syslog]);

    }
}